﻿using UnityEngine;
using System.Collections;

public class HealthPickup:MonoBehaviour {


    public float healthBonuts;
    public AudioClip collect;

    private PickupSpawner pickupSpawner;
    private Animator anim;
    private bool landed;

    private void Awake() {
        pickupSpawner = GameObject.Find("pickupManager").GetComponent<PickupSpawner>();
        anim = transform.root.GetComponent<Animator>();

    }

    private void OnTriggerEnter2D(Collider2D other) {

        if (other.tag == "Player") {
            PlayerHealth playerHealth = other.GetComponent<PlayerHealth>();

            playerHealth.health += healthBonuts;
            // 限制0 - 100
            playerHealth.health = Mathf.Clamp(playerHealth.health,0f,100f);

            playerHealth.UpdateHealthBar();

            pickupSpawner.StartCoroutine(pickupSpawner.DeliverPickup());

            AudioSource.PlayClipAtPoint(collect,transform.position);


            Destroy(transform.root.gameObject);

        } else if (other.tag == "ground" && !landed) {

            anim.SetTrigger("Land");

            transform.parent = null;
            gameObject.AddComponent<Rigidbody2D>();
            landed = true;

        }

    }

}
